﻿
Partial Class WebForm_ManageSystemParameter_AddSystemParameter
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fvParameter.InsertItemTemplate = Me.fvParameter.ItemTemplate
        Me.fvParameter.EditItemTemplate = Me.fvParameter.ItemTemplate
    End Sub

    Protected Sub fvParameter_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvParameter.DataBound
        ''
        ''
    End Sub

    Protected Sub fvParameter_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fvParameter.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If
        End If
    End Sub

End Class

Partial Class WebForm_ManageSystemParameter_AddSystemParameter
    Public Sub InitData()
        Me.fvParameter.ChangeMode(FormViewMode.Insert)
    End Sub

    Public Sub InitData(ByVal parameterType As String, ByVal parameterName As String)
        Me.fvParameter.ChangeMode(FormViewMode.Edit)
        Me.ParameterType = parameterType
        Me.ParameterName = parameterName
        Me.BindData()
    End Sub

    Private Sub BindData()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select * ")
        sql.AppendFormat(" from SystemParameter a inner join CodeMapping b  ")
        sql.AppendFormat(" on a.ParameterType = b.Value ")
        sql.AppendFormat(" where b.TableName='SystemParameter' and b.ColumnName='ParameterType' ")
        sql.AppendFormat(" and a.ParameterType = '{0}' ", Me.ParameterType)
        sql.AppendFormat(" and a.ParameterName = '{0}' ", Me.ParameterName)


        dt = dat.GetData(sql)

        Me.fvParameter.DataSource = dt
        Me.fvParameter.DataBind()
    End Sub

    Private Function SaveData() As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim ParameterValue As String = TryCast(fvParameter.FindControl("txtParameterValue"), TextBox).Text
        Using scope As New Transactions.TransactionScope
            '關帳日的參數需限定值爲數字
            If Me.ParameterType = "Data" AndAlso (Me.ParameterName = "CloseDay" Or Me.ParameterName = "CloseMonth") Then
                If IsNumeric(ParameterValue.ToString.Trim()) = False OrElse Int32.Parse(ParameterValue) <= 0 Then
                    MyObj.ShowMessage("此參數需輸入數值，且不可爲負數或零！")
                    TryCast(fvParameter.FindControl("txtParameterValue"), TextBox).Text = ""
                    Return False
                ElseIf Me.ParameterName = "CloseDay" AndAlso Int32.Parse(ParameterValue) > 31 Then
                    MyObj.ShowMessage("此參數不可以大於31！")
                    TryCast(fvParameter.FindControl("txtParameterValue"), TextBox).Text = ""
                    Return False
                ElseIf Me.ParameterName = "CloseMonth" AndAlso Int32.Parse(ParameterValue) > 12 Then
                    MyObj.ShowMessage("此參數不可以大於12！")
                    TryCast(fvParameter.FindControl("txtParameterValue"), TextBox).Text = ""
                    Return False
                End If

            End If
            sql.Length = 0
            sql.AppendFormat("update SystemParameter set ")
            sql.AppendFormat("ParameterValue='{0}' ", TryCast(fvParameter.FindControl("txtParameterValue"), TextBox).Text)
            sql.AppendFormat(",ParameterText='{0}' ", TryCast(fvParameter.FindControl("txtParameterText"), TextBox).Text)
            sql.AppendFormat(" where ParameterType = '{0}' ", Me.ParameterType)
            sql.AppendFormat(" and ParameterName = '{0}' ", Me.ParameterName)
            dat.AccData(sql)

            scope.Complete()
        End Using

        Return True
    End Function


    Public Property ParameterType() As String
        Get
            Return ViewState("ParameterType")
        End Get
        Set(ByVal value As String)
            ViewState("ParameterType") = value
        End Set
    End Property
    Public Property ParameterName() As String
        Get
            Return ViewState("ParameterName")
        End Get
        Set(ByVal value As String)
            ViewState("ParameterName") = value
        End Set
    End Property

    Public Event Saved(ByVal source As Object, ByVal e As EventArgs)

End Class
